

import { BeforeInsert, BeforeUpdate, Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { hashSync} from 'bcryptjs'
import { Exclude } from 'class-transformer';
import { UserInfoDto } from '../dto/create-user.dto';

@Entity('user')
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: number;

  @Column({ length: 100 })
  username: string; // 用户名

  @Column({ length: 100 })
  nickname: string;  //昵称

  @Column()
  @Exclude()
  password: string;  // 密码

  @Column()
  avatar: string;   //头像

  @Column()
  number: string; // 电话号码

  @Column('simple-enum', { enum: ['coach', 'admin', 'trainees'] })
  role: string;   // 用户角色

  @Column({
    name: 'create_time',
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;

  @Column({
    name: 'update_time',
    type: 'timestamp',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;
  
  @BeforeInsert() 
  async encryptPwd() { 
    this.password = await hashSync(this.password); 
  } 
  @BeforeUpdate()
  async UpdatePwd(){
    this.password = await hashSync(this.password); 
  }

  toResponseObject(): UserInfoDto {
    let responseObj: UserInfoDto = {
      ...this,
    };
    return responseObj;
  }
}
